var dataEntry;

$(function(){
  

	//初始化收入支出多选框
	$('#income_expense').select2({
       placeholder: '选择收支类型',
		  allowClear:true,
		  data:[{ id: '0', text: '收入' },{ id: '1', text: '支出' }],
		  language: "zh-CN"
     });
	
	//初始化账目类型下拉列表	
	 var acctTypeSelect= $('#acct_type').select2({
	        placeholder: '选择账目类型',
	        allowClear:true,
	        language: "zh-CN"
	    });
	 
	//初始化来源下拉列表	
	 var sourceSelect= $('#source').select2({
	        placeholder: '选择账目来源',
	        allowClear:true,
	        language: "zh-CN"
	    });
			
	/**
	 * 初始化页面的下拉列表
	 */
	var selectList = {
			
		initSelectList :function(){	
		$.ajax({
	      type: "post",
	      url: ctxPath+"/dataRecord/listAcctType.do",
	      dataType:"json",
	      data:{"inOutFlag":$("#income_expense").val()},
	      success : function(result){
	    	  acctTypeSelect.empty();
	    	  acctTypeSelect.append("<option value=''>选择账目类型</option>");
	    	  for(var i in result){
	    		  acctTypeSelect.append("<option value='"+result[i].typecode+"'>"+result[i].typename+"</option>");
	    	  }

	      }
	    });
		
		
		$.ajax({
	      type: "post",
	      url: ctxPath+"/dataRecord/listSource.do",
	      dataType:"json",
	      success : function(result){
	    	  sourceSelect.empty();
	    	  sourceSelect.append("<option value=''>选择账目来源</option>");
	    	  for(var i in result){
	    		  sourceSelect.append("<option value='"+result[i].departmentno+"'>"+result[i].departmentname+"</option>");
	    	  }

	      }
	    });
		
		
		
			
		}
	}	
	$('#income_expense').change(function(){
		
	selectList.initSelectList();
	})
	
	
	//日期格式化
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
	
	//初始化时间控件
	var max = new Date();
	laydate.render({
		elem:'#occurTime',
		max:max.Format('yyyy-MM-dd'),
		value: max.Format("yyyy-MM-dd")
		
	});
	
	
});



//重置方法
     function reset(){
	    $("#income_expense").val('选择收支类型').trigger("change");
		 $("#acct_type").val('选择账目类型').trigger("change");
		 $('#source').val('选择账目来源').trigger("change");
		 $('#occurTime').val("");
		 $('#money').val("");
		
   }

	/**
	 * 执行条件重置
	 */
	$("#reset_btn").on("click", function() { 
		reset();
	});
		
	//数据录入
	function input_data(){
		var myform =document.getElementById('myform');
		$myform = $(myform);
		//验证输入的注册信息 
		validateAll($myform);
		//将账号的不符原因更新
		if(validResult){	
			var income_expense=$("#income_expense").val();
			var acct_type=$("#acct_type").val();
			var occurTime=$("#occurTime").val();
			var money=$("#money").val();
			var source=$("#source").val();
			
			//进行资金流水录入	
			$.ajax({
				data : {"incomeExpense": income_expense,"acctType":acct_type,
					"occurTime":occurTime,"money":money,"source":source},
		  		type: "post",
		  		url: ctxPath + "/dataRecord/dataEntry.do",
		  		dataType:"json",
		  		success : function(data){  				
					if(data.status='0'){
						layer.alert("数据录入成功！");
						reset();
					}
					else{
						layer.alert("数据录入失败！");
						reset();
					}
		  		}
			});
		}

		
	}
	
	
	
	
	
//验证录入信息
	function validateAll(elem){
		   validResult = false; 
			/*收支类型验证incomeExpense*/
			var incomeExpense = $("#income_expense").val();
			if(!incomeExpense){
				validResult = false;					
				layer.alert("请选择收支类型！");
				return;
			}			
			/*来源验证source*/
			   var source=$("#source").val();
			   if(!source){
					validResult = false;					
					layer.alert("请输入来源！");
					return;
				}
			/*账目类型验证acctType*/
			var acctType = $("#acct_type").val();
			if(!acctType){
				validResult = false;					
				layer.alert("请选择账目类型！");
				return;
			}
			/*日期验证occurTime*/
			   var occurtime=$("#occurTime").val();
			   if(!occurtime){
					validResult = false;					
					layer.alert("请选择日期和时间！");
					return;
				} 		

			/*金额验证money*/
			  var money=$("#money").val();
			  if(!money){
					validResult = false;					
					layer.alert("请输入金额！");
					return;
				}
				var trade_money = money;
				var reg = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
				if(!reg.test(trade_money)){
					layer.alert("请输入正确金额且不超过两位小数！");
					return	false;
				   }
			
			
				validResult = true;
			
		return validResult;
	}





	
	


